package shweixin

import (
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/goods"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	. "yundian/internel/pkg/dingo/result"
)

//查询商品列表
func HandleGoodsList(c *gin.Context) {
	param := &request.GoodsListReq{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	if param.BuyType == 0 {
		param.BuyType = 1
	}
	data := dao.NewGoodsDao().GetGoodsList(param)

	Response(c)("", CodeSuccess, ResponseConfigList{
		Host:       global.Cfg.Ali.AliossPath,
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// 查看商品详情
func HandleGoodsDetails(c *gin.Context) {
	userId, _ := helper.GetUserIdFromCtx(c)
	param := &request.GoodsListReq{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	param.MemberId = userId

	notificationDetails := dao.NewGoodsDao().GetGoodsDetails(param).Unwrap().(*goods.Goods)
	Response(c)("", CodeSuccess, notificationDetails)(OK)
	return
}
